package com.voicechat.firstmet.adapter;


import android.content.Context;
import android.os.Bundle;
import android.os.Handler;
import android.os.Message;
import android.support.v7.widget.RecyclerView;
import android.text.TextUtils;
import android.view.View;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.TextView;

import com.voicechat.firstmet.R;
import com.voicechat.firstmet.base.BaseAdapter;
import com.voicechat.firstmet.bean.FansAndLike;
import com.voicechat.firstmet.client.OssClient;
import com.voicechat.firstmet.client.OssConfig;
import com.voicechat.firstmet.utils.GlideLoadUtils;
import com.voicechat.firstmet.utils.Utils;
import com.voicechat.firstmet.view.CircleImageView;

import java.io.IOException;
import java.io.InputStream;
import java.util.List;

import butterknife.BindView;
import butterknife.ButterKnife;

/**
 * @ProjectName: FirstMet
 * @Package: com.voicechat.firstmet.adapter
 * @ClassName: IncomeRecordAdapter
 * @Description: 我的粉丝和我关注的adapter
 * @Author: ljp
 * @CreateDate: 2018/10/11 10:01
 * @Version: 1.0
 */
public class FansAndFocusAdapter extends BaseAdapter<FansAndLike.PageBean.ListBean> {
    private List<FansAndLike.PageBean.ListBean> tlist;
    private ItemOnClick itemOnClick;
    private Handler handler = new Handler() {
        @Override
        public void handleMessage(Message msg) {
            if (msg.what == 1) {
                Bundle data = msg.getData();
                byte[] bitmap = data.getByteArray("bitmap");
                //String tag = data.getString("tag");
                CircleImageView view = (CircleImageView) msg.obj;
                GlideLoadUtils.getInstance().glideLoad(context,bitmap,view);
                /*GlideApp.with(context)
                        .load(bitmap)
                        .skipMemoryCache(true)
                        .diskCacheStrategy(DiskCacheStrategy.ALL)
                        .placeholder(R.drawable.ic1)
                        .into(view);*/
            }
        }
    };
    /**
     * @param context activity_imessage
     * @param tlist   集合
     * @param mLayout 主布局
     */
    public FansAndFocusAdapter(Context context, List<FansAndLike.PageBean.ListBean> tlist, int mLayout) {
        super(context, tlist, mLayout);
        this.tlist = tlist;
    }

    public void setItemOnClick(ItemOnClick itemOnClick) {
        this.itemOnClick = itemOnClick;
    }

    @Override
    protected RecyclerView.ViewHolder getHeaderViewHolder(View view) {
        return null;
    }

    @Override
    protected RecyclerView.ViewHolder getItemViewHolder(View view) {
        return new ViewHolder(view);
    }

    @Override
    protected RecyclerView.ViewHolder getFooterViewHolder(View view) {
        return null;
    }

    @Override
    public void onBindViewHolder(RecyclerView.ViewHolder viewHolder, int position) {
        if(viewHolder instanceof ViewHolder){
            ViewHolder itemViewHolder = (ViewHolder) viewHolder;
            FansAndLike.PageBean.ListBean listBean = tlist.get(position);
            //setImage(listBean.getAvatarUrl(),itemViewHolder.imageIcon);
            if(!TextUtils.isEmpty(listBean.getAvatarUrl())) {
                if (!listBean.getAvatarUrl().equals(itemViewHolder.imageIcon.getTag(R.id.image_icon))) {
                    // 加载图片
                    itemViewHolder.imageIcon.setTag(R.id.image_icon, listBean.getAvatarUrl());
                    GlideLoadUtils.getInstance().glideLoad(context, OssConfig.OSS_HTTP + listBean.getAvatarUrl(),
                            itemViewHolder.imageIcon);
                    //setImage(listBean.getAvatarUrl(), moCardViewHolder.imageIcon);
                }
            }
            if(!TextUtils.isEmpty(listBean.getGender())) {
                if (listBean.getGender().equals("1")) {//判断性别  1男 2女
                    itemViewHolder.imageSex.setImageResource(R.mipmap.boy);
                    itemViewHolder.linearSexAge.setBackgroundResource(R.mipmap.sex_blue);
                } else if (listBean.getGender().equals("2")) {
                    itemViewHolder.imageSex.setImageResource(R.mipmap.girl);
                    itemViewHolder.linearSexAge.setBackgroundResource(R.mipmap.sex_red);
                }
            }
            if (!TextUtils.isEmpty(listBean.getUType()) && listBean.getUType().equals("2")) {//判断是否是主播
                itemViewHolder.imageIsanchor.setVisibility(View.VISIBLE);
            } else {
                itemViewHolder.imageIsanchor.setVisibility(View.GONE);
            }
            if (!TextUtils.isEmpty(tlist.get(position).getBirthday())) {
                itemViewHolder.tvAge.setText(Utils.getAge(tlist.get(position).getBirthday()));
            }
            if(!TextUtils.isEmpty(listBean.getVType())){
                if (listBean.getVType().equals("1")) {//判断是vip等级 1 月 2 年 0 不是会员
                    itemViewHolder.imageVip.setVisibility(View.VISIBLE);
                    itemViewHolder.imageVip.setImageResource(R.mipmap.vip_month);
                } else if (listBean.getVType().equals("2")) {
                    itemViewHolder.imageVip.setVisibility(View.VISIBLE);
                    itemViewHolder.imageVip.setImageResource(R.mipmap.vip_year);
                } else {
                    itemViewHolder.imageVip.setVisibility(View.GONE);
                }
            }
            itemViewHolder.tvNikeName.setText(listBean.getNickName());
            itemViewHolder.tvSignText.setText(listBean.getAutograph());
            if(listBean.getState().equals("1")){//1 未关注 2已经关注 3互相关注
                itemViewHolder.tvState.setText("关注");
                itemViewHolder.tvState.setBackgroundResource(R.drawable.login_btn);
            }else if(listBean.getState().equals("2")){
                itemViewHolder.tvState.setText("已关注");
                itemViewHolder.tvState.setBackgroundResource(R.drawable.login_btn_2);
            }else if(listBean.getState().equals("3")){
                itemViewHolder.tvState.setText("互相关注");
                itemViewHolder.tvState.setBackgroundResource(R.drawable.login_btn_2);
            }
            itemViewHolder.tvState.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View v) {
                    itemOnClick.setStateOnClick(position,v);
                }
            });
            itemViewHolder.itemView.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View v) {
                    itemOnClick.setItemOnClick(position,v);
                }
            });
            itemViewHolder.imageIcon.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View v) {
                    itemOnClick.setIconOnClick(listBean,v);
                }
            });
        }

    }
    private void setImage(String url, View view) {
        new Thread(new Runnable() {
            @Override
            public void run() {
                OssClient.getInstance().init(context).fileDownload(url, new OssClient.OnDownloadFile() {
                    @Override
                    public void onDownFileSuccess(InputStream inputStream) {
                        byte[] bitmap = null;
                        try {
                            bitmap = OssConfig.readInputStreamToByte(inputStream);
                        } catch (IOException e) {
                            e.printStackTrace();
                        }
                        //if(view.getTag() != null && view.getTag().equals(url)) {
                        Message message = Message.obtain();
                        message.what = 1;
                        message.obj = view;
                        Bundle bundle = new Bundle();
                        //bundle.putString("tag",url);
                        bundle.putByteArray("bitmap", bitmap);
                        message.setData(bundle);
                        handler.sendMessage(message);
                        //  }
                    }

                    @Override
                    public void onDownFileProgress(String progress) {

                    }
                });
            }
        }).start();

    }
    class ViewHolder extends BaseViewHolder{
        @BindView(R.id.image_icon)
        CircleImageView imageIcon;
        @BindView(R.id.tv_nike_name)
        TextView tvNikeName;
        @BindView(R.id.image_sex)
        ImageView imageSex;
        @BindView(R.id.tv_age)
        TextView tvAge;
        @BindView(R.id.linear_sex_age)
        LinearLayout linearSexAge;
        @BindView(R.id.image_vip)
        ImageView imageVip;
        @BindView(R.id.tv_state)
        TextView tvState;
        @BindView(R.id.tv_sign_text)
        TextView tvSignText;
        View itemView;
        @BindView(R.id.image_isanchor)
        ImageView imageIsanchor;
        public ViewHolder(View itemView) {
            super(itemView);
            ButterKnife.bind(this,itemView);
            this.itemView = itemView;
        }
    }
    public interface ItemOnClick{
        void setItemOnClick(int position,View view);
        void setStateOnClick(int position,View view);
        void setIconOnClick(FansAndLike.PageBean.ListBean listBean,View view);
    }
}
